﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;

namespace CustomsSystem.DAL
{
    public class WorkflowService
    {
        /// <summary>
        /// 得到最大ID
        /// </summary>
        /*public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("infoWKID", "Info_workflow");
        }*/


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int infoWKID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Info_workflow");
            strSql.Append(" where infoWKID=" + infoWKID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Info_workflow model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.infoWKTypeID != null)
            {
                strSql1.Append("infoWKTypeID,");
                strSql2.Append("" + model.infoWKTypeID + ",");
            }
            if (model.stepID != null)
            {
                strSql1.Append("stepID,");
                strSql2.Append("" + model.stepID + ",");
            }
            if (model.busType != null)
            {
                strSql1.Append("busType,");
                strSql2.Append("" + model.busType + ",");
            }
            if (model.busName != null)
            {
                strSql1.Append("busName,");
                strSql2.Append("'" + model.busName + "',");
            }
            if (model.jobNo != null)
            {
                strSql1.Append("jobNo,");
                strSql2.Append("'" + model.jobNo + "',");
            }
            if (model.doTimeLimit != null)
            {
                strSql1.Append("doTimeLimit,");
                strSql2.Append("" + model.doTimeLimit + ",");
            }
            if (model.yellowBefore != null)
            {
                strSql1.Append("yellowBefore,");
                strSql2.Append("" + model.yellowBefore + ",");
            }
            if (model.remark != null)
            {
                strSql1.Append("remark,");
                strSql2.Append("'" + model.remark + "',");
            }
            if (model.remark != null)
            {
                strSql1.Append("States,");
                strSql2.Append("" + model.States + ",");
            }
            if (model.remark1 != null)
            {
                strSql1.Append("remark1,");
                strSql2.Append("'" + model.remark1 + "',");
            }
            if (model.remark2 != null)
            {
                strSql1.Append("remark2,");
                strSql2.Append("'" + model.remark2 + "',");
            }
            strSql.Append("insert into Info_workflow(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int AddMaster(CustomsSystem.Model.Info_workflowMaster model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.sheetTypeID != null)
            {
                strSql1.Append("sheetTypeID,");
                strSql2.Append("" + model.sheetTypeID + ",");
            }
            if (model.goodsTypeID != null)
            {
                strSql1.Append("goodsTypeID,");
                strSql2.Append("" + model.goodsTypeID + ",");
            }
            if (model.WKTypeName != null)
            {
                strSql1.Append("WKTypeName,");
                strSql2.Append("'" + model.WKTypeName + "',");
            }
            if (model.States != null)
            {
                strSql1.Append("States,");
                strSql2.Append("'" + model.States + "',");
            }
            strSql.Append("insert into Info_workflowMaster(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Info_workflow model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Info_workflow set ");
            if (model.infoWKTypeID != null)
            {
                strSql.Append("infoWKTypeID=" + model.infoWKTypeID + ",");
            }
            if (model.stepID != null)
            {
                strSql.Append("stepID=" + model.stepID + ",");
            }
            if (model.busType != null)
            {
                strSql.Append("busType=" + model.busType + ",");
            }
            if (model.busName != null)
            {
                strSql.Append("busName='" + model.busName + "',");
            }
            else
            {
                strSql.Append("busName= null ,");
            }
            if (model.jobNo != null)
            {
                strSql.Append("jobNo='" + model.jobNo + "',");
            }
            else
            {
                strSql.Append("jobNo= null ,");
            }
            if (model.doTimeLimit != null)
            {
                strSql.Append("doTimeLimit=" + model.doTimeLimit + ",");
            }
            else
            {
                strSql.Append("doTimeLimit= null ,");
            }
            if (model.yellowBefore != null)
            {
                strSql.Append("yellowBefore=" + model.yellowBefore + ",");
            }
            else
            {
                strSql.Append("yellowBefore= null ,");
            }
            if (model.remark != null)
            {
                strSql.Append("remark='" + model.remark + "',");
            }
            else
            {
                strSql.Append("remark= null ,");
            }
            if (model.States != null)
            {
                strSql.Append("States=" + model.States + ",");
            }
            else
            {
                strSql.Append("States= null ,");
            }
            if (model.remark1 != null)
            {
                strSql.Append("remark1='" + model.remark1 + "',");
            }
            else
            {
                strSql.Append("remark1= null ,");
            }
            if (model.remark2 != null)
            {
                strSql.Append("remark2='" + model.remark2 + "',");
            }
            else
            {
                strSql.Append("remark2= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where infoWKID=" + model.infoWKID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateMaster(CustomsSystem.Model.Info_workflowMaster model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Info_workflowMaster set ");
            if (model.sheetTypeID != null)
            {
                strSql.Append("sheetTypeID=" + model.sheetTypeID + ",");
            }
            else
            {
                strSql.Append("sheetTypeID= null ,");
            }
            if (model.goodsTypeID != null)
            {
                strSql.Append("goodsTypeID=" + model.goodsTypeID + ",");
            }
            else
            {
                strSql.Append("goodsTypeID= null ,");
            }
            if (model.WKTypeName != null)
            {
                strSql.Append("WKTypeName='" + model.WKTypeName + "',");
            }
            else
            {
                strSql.Append("WKTypeName= null ,");
            }
            if (model.States != null)
            {
                strSql.Append("States='" + model.States + "',");
            }
            else
            {
                strSql.Append("States= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where infoWKTypeID=" + model.infoWKTypeID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int infoWKID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_workflow ");
            strSql.Append(" where infoWKID=" + infoWKID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteMaster(int infoWKTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_workflowMaster ");
            strSql.Append(" where infoWKTypeID=" + infoWKTypeID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string infoWKIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_workflow ");
            strSql.Append(" where infoWKID in (" + infoWKIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_workflow GetModel(int infoWKID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" infoWKID,infoWKTypeID,stepID,busType,busName,jobNo,doTimeLimit,yellowBefore,remark,States ");
            strSql.Append(" from Info_workflow ");
            strSql.Append(" where infoWKID=" + infoWKID + "");
            CustomsSystem.Model.Info_workflow model = new CustomsSystem.Model.Info_workflow();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public CustomsSystem.Model.Info_workflowMaster GetModelMaster(int infoWKTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" infoWKTypeID,sheetTypeID,goodsTypeID,WKTypeName,States ");
            strSql.Append(" from Info_workflowMaster ");
            strSql.Append(" where infoWKTypeID=" + infoWKTypeID + "");
            CustomsSystem.Model.Info_workflowMaster model = new CustomsSystem.Model.Info_workflowMaster();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModelMaster(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        public CustomsSystem.Model.Info_workflowMaster DataRowToModelMaster(DataRow row)
        {
            CustomsSystem.Model.Info_workflowMaster model = new CustomsSystem.Model.Info_workflowMaster();
            if (row != null)
            {
                if (row["infoWKTypeID"] != null && row["infoWKTypeID"].ToString() != "")
                {
                    model.infoWKTypeID = int.Parse(row["infoWKTypeID"].ToString());
                }
                if (row["sheetTypeID"] != null && row["sheetTypeID"].ToString() != "")
                {
                    model.sheetTypeID = int.Parse(row["sheetTypeID"].ToString());
                }
                if (row["goodsTypeID"] != null && row["goodsTypeID"].ToString() != "")
                {
                    model.goodsTypeID = int.Parse(row["goodsTypeID"].ToString());
                }
                if (row["WKTypeName"] != null)
                {
                    model.WKTypeName = row["WKTypeName"].ToString();
                }
                if (row["States"] != null && row["States"].ToString() != "")
                {
                    model.States = int.Parse(row["States"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_workflow DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Info_workflow model = new CustomsSystem.Model.Info_workflow();
            if (row != null)
            {
                if (row["infoWKID"] != null && row["infoWKID"].ToString() != "")
                {
                    model.infoWKID = int.Parse(row["infoWKID"].ToString());
                }
                if (row["infoWKTypeID"] != null && row["infoWKTypeID"].ToString() != "")
                {
                    model.infoWKTypeID = int.Parse(row["infoWKTypeID"].ToString());
                }
                if (row["stepID"] != null && row["stepID"].ToString() != "")
                {
                    model.stepID = int.Parse(row["stepID"].ToString());
                }
                if (row["busType"] != null && row["busType"].ToString() != "")
                {
                    model.busType = int.Parse(row["busType"].ToString());
                }
                if (row["busName"] != null)
                {
                    model.busName = row["busName"].ToString();
                }
                if (row["jobNo"] != null)
                {
                    model.jobNo = row["jobNo"].ToString();
                }
                if (row["doTimeLimit"] != null && row["doTimeLimit"].ToString() != "")
                {
                    model.doTimeLimit = int.Parse(row["doTimeLimit"].ToString());
                }
                if (row["yellowBefore"] != null && row["yellowBefore"].ToString() != "")
                {
                    model.yellowBefore = int.Parse(row["yellowBefore"].ToString());
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }

                if (row["States"] != null && row["States"].ToString() != "")
                {
                    model.States =int.Parse( row["States"].ToString());
                }
            }
            return model;
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select infoWKID,infoWKTypeID,stepID,busType,busName,jobNo,doTimeLimit,yellowBefore,remark,States ");
            strSql.Append(" FROM Info_workflow ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetListMaster(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM Info_workflowMaster ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }



        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" infoWKID,infoWKTypeID,stepID,busType,busName,jobNo,doTimeLimit,yellowBefore,remark,States ");
            strSql.Append(" FROM Info_workflow ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Info_workflow ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
